#!/bin/sh

echo "========================================"
echo "启动企业级开发模板服务"
echo "========================================"

# 替换默认的 nginx 配置文件
cp ./nginx.conf /etc/nginx/nginx.conf

# 启动 nginx 服务
echo "启动 nginx 服务..."
screen -dmS nginx nginx -g "daemon off;"

# 启动 app-linux 服务
echo "启动 app-linux 服务..."
cd /app/server
screen -dmS app ./app-linux -c config.yaml
cd /app

# 启动 node 服务
echo "启动 node 服务..."
cd /app/nuxt
if [ -f "server/index.mjs" ]; then
    screen -dmS node node ./server/index.mjs
fi
cd /app

# 等待所有服务启动
sleep 3

# 显示运行中的 screen 会话
echo "当前运行的 screen 会话："
screen -list

echo "========================================"
echo "所有服务已启动完成！"
echo "========================================"
echo "服务访问地址："
echo "- Nginx: http://localhost:80"
echo "- Go API: http://localhost:8888"
echo "- Nuxt: http://localhost:3000"
echo "========================================"

echo "使用 'screen -r <session_name>' 来查看特定服务的日志"
echo "例如：screen -r nginx, screen -r app, screen -r node"

# 保持容器运行并监控服务状态
echo "容器已启动，正在监控服务状态..."
while true; do
    # 检查所有screen会话是否还在运行
    if ! screen -list | grep -q "nginx\|app\|node"; then
        echo "警告：某些服务已停止运行"
        screen -list
    fi
    
    # 每30秒检查一次服务状态
    sleep 30
done
